#pragma once
#include<string>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAX_BUFF_SIZE 1024

class InetAddr
{
private:
    //存储内核地址信息
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
private:

public:
    InetAddr(const std::string& ip,uint16_t port) : _ip(ip),_port(port)
    {
        _addr.sin_family = AF_INET;
        memset(&_addr,0,sizeof(_addr));
        ::inet_pton(AF_INET,ip.c_str(),&_addr.sin_addr.s_addr);
        _addr.sin_port = ::htons(_port);
    }
    InetAddr(const struct sockaddr_in& addr)
    {
        char buff[32];
        ::inet_ntop(AF_INET,&addr.sin_addr,buff,sizeof(buff));
        _port = ::ntohs(addr.sin_port);
        _ip = buff;
        memset(&_addr,0,sizeof(_addr));
        _addr.sin_addr.s_addr = addr.sin_addr.s_addr;
        _addr.sin_family = AF_INET;
        _addr.sin_port = addr.sin_port;
    }
    bool operator==(const InetAddr& other)
    {
        return (_ip == other._ip) && (_port == other._port);
    }
    InetAddr(const InetAddr& other)
    {
        _addr = other._addr;
        _ip = other._ip;
        _port = other._port;
    }
    std::string GetInetAddrStr()
    {
        char str[MAX_BUFF_SIZE];
        sprintf(str,"[ip:%s][port:%d]",_ip.c_str(),_port);
        std::string res = str;
        return res;
    }
    uint16_t Port()
    {
        return _port;
    }
    std::string IP()
    {
        return _ip;
    }
    struct sockaddr_in GetAddr()
    {
        return _addr;
    }
    ~InetAddr()
    {}
};